package org.nf.box.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.nf.box.domain.Comment;
import org.nf.box.domain.PersonalInfo;
import org.nf.box.domain.User;
import org.nf.box.mapper.CommentLikeMapper;
import org.nf.box.mapper.CommentMapper;
import org.nf.box.mapper.SubCommentMapper;
import org.nf.box.mapper.UserMapper;
import org.nf.box.service.CommentService;
import org.nf.box.service.PersonalInfoService;
import org.nf.box.service.UserService;
import org.nf.box.web.vo.CommentVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * @Author lzp
 * @Date 2024-12-19
 * 评论业务
 */
@SuppressWarnings("UnreachableCode")
@Service("commentService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class CommentServiceImpl implements CommentService {

    private final CommentMapper commentMapper;
    private final PersonalInfoService personalInfoService;
    private final CommentLikeMapper commentLikeMapper;
    @Override
    public void addComment(Comment comment) {
        commentMapper.addComment(comment);
    }

    @Override
    public List<CommentVO> getComments(Integer postId) {
        List<CommentVO> comments = commentMapper.getParentComment(postId);
        for (CommentVO comment : comments) {
            //获取个人资料并设置头像昵称
            PersonalInfo personalInfo = getPersonalInfo(comment);
            if (personalInfo != null) {
                comment.setHeadPortrait(personalInfo.getHeadPortrait());
                comment.setNickname(personalInfo.getNickName());
            }
            //获取并设置评论点赞数
            Integer commentCount = commentLikeMapper.getParentCommentCount(comment.getCommentId());
            comment.setLikeCount(commentCount);
            //获取并设置回复数
            Integer subCommentCount = commentMapper.getSubCommentCount(comment.getCommentId());
            comment.setSubCommentCount(subCommentCount);
            //获取并设置是否点赞
            Integer isLink = commentMapper.getIsLink(comment.getCommentId(), comment.getUserId());
            comment.setIsLink(isLink);
            //获取并设置总评论数
//            Integer totalComments = commentMapper.getTotalComment(comment.getPostId());
//            comment.setTotalComments(totalComments);
        }
        return comments;
    }

    /**
     * 获取个人资料
     * @param comment
     * @return
     */
    private PersonalInfo getPersonalInfo(CommentVO comment) {
        return personalInfoService.getPersonalInfoById(comment.getUserId());
    }

    @Override
    public CommentVO getParentCommentById(Integer postId, Integer commentId) {
        CommentVO parentComment = commentMapper.getParentCommentById(postId, commentId);
        //获取个人资料并设置头像昵称
        PersonalInfo personalInfo = getPersonalInfo(parentComment);
        if (personalInfo != null) {
            parentComment.setHeadPortrait(personalInfo.getHeadPortrait());
            parentComment.setNickname(personalInfo.getNickName());
        }
        //获取并设置评论点赞数
        Integer commentCount = commentLikeMapper.getParentCommentCount(parentComment.getCommentId());
        parentComment.setLikeCount(commentCount);
        //获取并设置回复数
        Integer subCommentCount = commentMapper.getSubCommentCount(parentComment.getCommentId());
        parentComment.setSubCommentCount(subCommentCount);
        //获取并设置是否点赞
        Integer isLink = commentMapper.getIsLink(parentComment.getCommentId(), parentComment.getUserId());
        parentComment.setIsLink(isLink);
        return parentComment;
    }

    @Override
    public Integer getTotalComment(Integer postId) {
        return commentMapper.getTotalComment(postId);
    }
//    @Override
//    public List<CommentVO> getComments(Integer postId) {
//        // 获取所有顶级评论
//        List<CommentVO> comments = commentMapper.getParentComment(postId);
//        // 收集所有需要查询的用户ID
//        Set<Integer> userIds = new HashSet<>();
//        Set<Integer> commentIds = new HashSet<>();
//        for (CommentVO comment : comments) {
//            userIds.add(comment.getUserId());
//            commentIds.add(comment.getCommentId());
//        }
//        // 批量获取用户资料
//        Map<Integer, PersonalInfo> personalInfoMap = personalInfoService.getPersonalInfosByIds(new ArrayList<>(userIds));
//        // 批量获取评论点赞数
//        Map<Integer, Integer> commentLikeCounts = commentLikeMapper.getParentCommentCounts(new ArrayList<>(commentIds));
//        // 批量获取回复数
//        Map<Integer, Integer> subCommentCounts = commentMapper.getSubCommentCounts(new ArrayList<>(commentIds));
//        // 批量获取是否点赞
//        Map<Integer, Integer> isLinkMap = commentMapper.getIsLinks(new ArrayList<>(commentIds));
//        // 批量获取总评论数
//        Map<Integer, Integer> totalCommentsMap = commentMapper.getTotalComments(new ArrayList<>(commentIds));
//        // 填充评论信息
//        for (CommentVO comment : comments) {
//            PersonalInfo personalInfo = personalInfoMap.get(comment.getUserId());
//            if (personalInfo != null) {
//                comment.setHeadPortrait(personalInfo.getHeadPortrait());
//                comment.setNickname(personalInfo.getNickName());
//            }
//            comment.setLikeCount(commentLikeCounts.get(comment.getCommentId()));
//            comment.setSubCommentCount(subCommentCounts.get(comment.getCommentId()));
//            comment.setIsLink(isLinkMap.get(comment.getCommentId()));
//            comment.setTotalComments(totalCommentsMap.get(comment.getCommentId()));
//        }
//        return comments;
//    }

}
